
import {flvCommon} from '../../common/utils'
import {playerEvents} from '../../playerEvents'
import EventEmitter from '../../common/EventEmitter'
const flv = window.flvjs
const DPlayer = window.DPlayer
export default class DflvPlayer{
	constructor(videoparam,el) {
		if(typeof(el) === 'string' && document.getElementById(el)){
			this.element = document.getElementById(el)
		}else{
			console.error('el参数为元素ID,该参数必传!')
		}
		this.emitter = new EventEmitter()
	  this.player = new DPlayer({
	   	element: this.element,
			theme: '#1090fe',
	   	autoplay: true,
	   	live: true,
	   	loop: false,
	   	lang: 'zh-cn',
	   	preload: 'auto',
	   	logo: videoparam.logo,
	   	video: {
	   		url: videoparam.url,
	   		pic: videoparam.pic,
	   		type: 'customFlv',
	   		customType: {
            'customFlv': (video, player) =>{
								this.flvPlayer = flv.createPlayer({
                    type: 'flv',
                    url: video.src
                });
                this.flvPlayer.attachMediaElement(video)
                this.flvPlayer.load()
								this.flvPlayer.on(flv.Events.ERROR,()=>{
									this.destroy()
									this.emitter.trigger(playerEvents.FLV_ERROR)
								})
            }
	   		}
	   	}
	  })
	}
	destroy(){
		flvCommon.destroyFlvPlayer(this.flvPlayer)
		this.player.destroy()
	}
	on(event, listener) {
		this.emitter.on(event, listener)
	}
	off(event, listener) {
		this.emitter.off(event, listener)
	}
}